local hx__shicai = fk.CreateSkill {
  name = "hx__shicai",
}


hx__shicai:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(hx__shicai.name) then return end
    local card_type = data.card.type
    if card_type == Card.TypeEquip then
      if not table.contains(player:getCardIds("e"), data.card:getEffectiveId()) then return end
    end
    if card_type == Card.TypeEquip then
      if player:getTableMark("@hx__shicai-turn")[1] ~= nil then
        return player:getTableMark("@hx__shicai-turn")[1] < 2
      else
        return true
      end
    elseif card_type == Card.TypeTrick then
      if data.card:isCommonTrick() then
        if player:getTableMark("@hx__shicai-turn")[2] ~= nil then
          return player:getTableMark("@hx__shicai-turn")[2] < 2
        else
          return true
        end
      end
    elseif card_type == Card.TypeBasic then
      if player:getTableMark("@hx__shicai-turn")[3] ~= nil then
        return player:getTableMark("@hx__shicai-turn")[3] < 2
      else
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = hx__shicai.name,
      prompt = "#hx__shicai-invoke:::"..data.card:toLogString()
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toPut = data.card:isVirtual() and data.card.subcards or { data.card.id }
    if #toPut > 1 then
      toPut = room:askToGuanxing(player, {
        cards = toPut,
        top_limit = { #toPut, #toPut },
        bottom_limit = { 0, 0 },
        skill_name = hx__shicai.name,
        skip = true,
      }).top
      toPut = table.reverse(toPut)
    end
    room:moveCardTo(toPut, Card.DrawPile, nil, fk.ReasonPut, hx__shicai.name, nil, true)
    player:drawCards(1, hx__shicai.name)
    local e
    local t
    local b
    if player:getTableMark("@hx__shicai-turn")[1] == nil then
      e = 0
    else
      e = player:getTableMark("@hx__shicai-turn")[1]
    end
    if player:getTableMark("@hx__shicai-turn")[2] == nil then
      t = 0
    else
      t = player:getTableMark("@hx__shicai-turn")[2]
    end
    if player:getTableMark("@hx__shicai-turn")[3] == nil then
      b = 0
    else
      b = player:getTableMark("@hx__shicai-turn")[3]
    end
    if data.card.type == Card.TypeEquip then
      e = e + 1
    elseif data.card.type == Card.TypeTrick then
      t = t + 1
    elseif data.card.type == Card.TypeBasic then
      b = b + 1
    end
    room:setPlayerMark(player, "@hx__shicai2-turn", {"装", "锦", "基"})
    room:setPlayerMark(player, "@hx__shicai-turn", {e, t, b})

  end,
})


return hx__shicai
